/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.geometry;

import java.util.HashSet;
import java.util.Set;

import org.openaecfm.model.geometry.CartesianPoint;
import org.openaecfm.model.geometry.Curve;
import org.openaecfm.model.geometry.TrimmedCurve;
import org.openaecfm.model.geometry.TrimmingPreference;
import org.openaecfm.model.measure.ParameterValue;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.TypedParameter;

public final class TrimmedCurveIO extends
		AbstractEntityInstanceInputImpl<TrimmedCurve> implements
		IEntityInstanceOutput<TrimmedCurve> {
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(TrimmedCurve pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCTRIMMEDCURVE",
				appDataManager.getName(pojo));

		// Set BasisCurve
		EntityInstanceNameParameter basis = new EntityInstanceNameParameter(
				appDataManager.getName(pojo.getBasisCurve()));
		ei.addParameter(basis);

		// Set Trim1
		ListParameter trim1 = new ListParameter();
		for (Object ts : pojo.getTrim1()) {
			if (ts instanceof CartesianPoint) {
				trim1.add(new EntityInstanceNameParameter(
						appDataManager.getName(ts)));
			} else {
				TypedParameter t = new TypedParameter("IFCPARAMETERVALUE",
						new RealParameter(((ParameterValue) ts).doubleValue()));
				trim1.add(t);
			}
		}
		ei.addParameter(trim1);

		// Set Trim2
		ListParameter trim2 = new ListParameter();
		for (Object ts : pojo.getTrim2()) {
			if (ts instanceof CartesianPoint) {
				trim2.add(new EntityInstanceNameParameter(
						appDataManager.getName(ts)));
			} else {
				TypedParameter t = new TypedParameter("IFCPARAMETERVALUE",
						new RealParameter(((ParameterValue) ts).doubleValue()));
				trim2.add(t);
			}

		}
		ei.addParameter(trim2);

		// Set SenseAgreement
		EnumerationParameter sense = new EnumerationParameter(
				pojo.getSenseAgreement());
		ei.addParameter(sense);

		// Set MasterRepresentation
		EnumerationParameter master = new EnumerationParameter(
				pojo.getMasterRepresentation());
		ei.addParameter(master);

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected TrimmedCurve doCreateObject() {
		return new TrimmedCurve();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, TrimmedCurve pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set BasisCurve
		EntityInstanceNameParameter basis = (EntityInstanceNameParameter) sei
				.getParameter(0);
		pojo.setBasisCurve((Curve) appDataManager.getObject(basis
				.toInstanceName()));

		// Set Trim1
		ListParameter trim1 = (ListParameter) sei.getParameter(1);
		Set<Object> l1 = new HashSet<Object>();
		for (Parameter p : trim1) {
			if (p.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
				EntityInstanceNameParameter n = (EntityInstanceNameParameter) p;
				Object entity = appDataManager.getObject(n.toInstanceName());
				l1.add(entity);
			}
			if (p.getNodeType() == NodeType.TYPED_PARAMETER) {
				TypedParameter t = (TypedParameter) p;
				RealParameter r = (RealParameter) t.getParameter();
				l1.add(new ParameterValue(r.toDouble()));
			}
		}
		pojo.setTrim1(l1);

		// Set Trim2
		ListParameter trim2 = (ListParameter) sei.getParameter(2);
		Set<Object> l2 = new HashSet<Object>();
		for (Parameter p : trim2) {
			if (p.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
				EntityInstanceNameParameter n = (EntityInstanceNameParameter) p;
				Object entity = appDataManager.getObject(n.toInstanceName());
				l2.add(entity);
			}
			if (p.getNodeType() == NodeType.TYPED_PARAMETER) {
				TypedParameter t = (TypedParameter) p;
				RealParameter r = (RealParameter) t.getParameter();
				l2.add(new ParameterValue(r.toDouble()));
			}
		}
		pojo.setTrim2(l2);
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			TrimmedCurve pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set SenseAgreement
		EnumerationParameter sense = (EnumerationParameter) sei.getParameter(3);
		pojo.setSenseAgreement(org.openaecfm.model.measure.Boolean
				.valueOf(sense.toEnumString()));

		// Set MasterRepresentation
		EnumerationParameter master = (EnumerationParameter) sei
				.getParameter(4);
		pojo.setMasterRepresentation(TrimmingPreference.valueOf(master
				.toEnumString()));
	}
}
